/**
 * Mobile
 * @version 1.0
 * @author David Ronai
 */
 
TO.Mobile = function( avatarImage, data, low ) {
	
	THREE.Object3D.call( this );

	this.avatarImage = avatarImage;	
	this.data = data;
	
	this.vx = 0;
	this.currentSpeed = 0;
	this.k = 0;
	this.modificator = 1;
	this.crazyOn = false;
	this.poped = false;
	
	//create avatar
	var matrix = new THREE.Matrix4();
	matrix.makeRotationX( Math.PI/2 );
	var geometry = new THREE.PlaneGeometry( 83, 83 );
	geometry.applyMatrix(matrix);
	
	var canvas = document.createElement( "canvas" );
		canvas.width = this.avatarImage.width;
		canvas.height = this.avatarImage.height;
	var context = canvas.getContext( "2d" );
		context.fillStyle = "rgba(0,0,0,255)";
		context.fillRect(0,0,this.avatarImage.width,this.avatarImage.height);
		context.drawImage(this.avatarImage, 0, 0,this.avatarImage.width,this.avatarImage.height);
		
	var texture = new THREE.Texture( canvas, THREE.UVMapping, THREE.RepeatWrapping, THREE.RepeatWrapping );
		texture.needsUpdate = true;
		
	this.mesh = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial( { map:texture } ));
	this.mesh.position.y = 66;
	this.mesh.position.z = 4;
	this.add(this.mesh);
	
	if(low){
		this.head = modelLoader.get("headLow");
		this.legRight = modelLoader.get("legLow");
		this.legLeft = modelLoader.get("legLow");
		this.armRight = modelLoader.get("armRightLow");
		this.armLeft = modelLoader.get("armLeftLow");

	} else {
		this.head = modelLoader.get("head");
		this.legRight = modelLoader.get("leg");
		this.legLeft = modelLoader.get("leg");
		this.armRight = modelLoader.get("armRight");
		this.armLeft = modelLoader.get("armLeft");
	}
	
	this.add(this.head);
	this.add(this.legRight);
	this.add(this.legLeft);
	this.add(this.armRight);
	this.add(this.armLeft);
	
	this.head.position.y = 52;
	this.legRight.position.y = 20;
	this.legRight.position.x = -14;
	this.legRight.position.z = -5;
	this.legLeft.position.y = 20;
	this.legLeft.position.x = 14;
	this.legLeft.position.z = -5;
	this.armRight.position.y = 55;
	this.armRight.position.x = -45;
	this.armRight.position.z = -3;
	this.armLeft.position.y = 55;
	this.armLeft.position.x = 45;
	this.armLeft.position.z = -3;
	
	if( this.data ){
		this.position.x = this.data.x;
		this.position.z = this.data.z;
	};
	
	this.initSpeed = function(averageSpeed, difference) {
		this.speeds = [];
		for( var i = 0; i<5; i++ ){
			var modificator = difference*Math.random();
			this.speeds[parseInt(i*2)] = averageSpeed - modificator;
			this.speeds[parseInt(i*2+1)] = averageSpeed + modificator;
		}
		var randomize = function (){
			return (Math.round(Math.random())-0.5); 
		}
		this.speeds = this.speeds.sort(randomize);
		this.changeSpeed(this);
	}
	
	this.changeSpeed = function ( scope ) {
		scope.currentSpeed = (scope.currentSpeed+1)%scope.speeds.length;
		TweenLite.to(scope,1,{vx:scope.speeds[scope.currentSpeed],onComplete:scope.changeSpeed, onCompleteParams:[scope]});
	}
	
	this.crazy = function() {
		var m = this;
		if(!this.crazyOn){
			TweenLite.to(this.armLeft.rotation,.1,{x:Math.PI});
			TweenLite.to(this.armRight.rotation,.1,{x:Math.PI});
			TweenLite.to(this.armLeft.rotation,.3,{x:Math.PI*2,delay:.45, ease:Quad.easeOut});
			TweenLite.to(this.armRight.rotation,.3,{x:Math.PI*2,delay:.45, ease:Quad.easeOut});
			TweenLite.to(this.rotation,.6,{y:this.rotation.y+Math.PI*2});
			TweenLite.to(this.position,.3,{y:this.position.y+5});
			TweenLite.to(this.position,.3,{delay:.3,y:this.position.y, onComplete:function(){m.crazyOn = false;}});
			this.crazyOn = true;
		}
	}

	this.pop = function() {
		if(!this.poped){
			var scale = 0.00000001;
			TweenLite.to(this.scale,.3,{x:scale,y:scale,z:scale,ease:Back.easeIn});
			this.poped = true;
		}
	}
	
	this.run = function () {
		var MAX_ANGLE = 35;
		this.position.x += this.vx;
		this.k += (this.vx*this.modificator/2);
		if( this.k < -MAX_ANGLE || this.k > MAX_ANGLE ){
			this.modificator *= -1;
		}
		this.legRight.rotation.x = this.k*Math.PI/180;
		this.legLeft.rotation.x = -this.k*Math.PI/180;
		this.armLeft.rotation.x = this.k*Math.PI/180;
		this.armRight.rotation.x = -this.k*Math.PI/180;
	}
	
	this.stopRun = function (){
		TweenLite.killTweensOf(this);
		TweenLite.to(this.armLeft.rotation,.2,{x:Math.PI*2,delay:.45, ease:Quad.easeOut});
		TweenLite.to(this.armRight.rotation,.2,{x:Math.PI*2,delay:.45, ease:Quad.easeOut});
	}
}

TO.Mobile.prototype = new THREE.Object3D();
TO.Mobile.prototype.constructor = TO.Mobile;